home *** CD-ROM | disk | FTP | other *** search
/ Aminet 21 / Aminet 21 (1997)(GTI - Schatztruhe)[!][Oct 1997].iso / Aminet / comm / bbs / cit_src_7H21.lha / libnet.c < prev    next >
C/C++ Source or Header  |  1997-07-27  |  4KB  |  191 lines

  1. /*
  2. *                               libnet.c
  3. *
  4. * Library net functions.
  5. */
  6. /*
  7. *                               history
  8. *
  9. * 85Nov15 HAW  Created.
  10. */
  11. #include "ctdl.h"
  12. NetBuffer        netBuf;
  13. NetBuffer        netTemp;
  14. extern NetTable  *netTab;
  15. extern CONFIG    cfg;
  16. int              thisNet = -1;
  17. FILE             *netfl;
  18. /*
  19. *                               contents
  20. *
  21. *       getNet()                gets a node from CTDLNET.SYS
  22. *       putNet()                puts a node to CTDLNET.SYS
  23. *       normId()                normalizes the ID
  24. *       searchNet()             search net on ID
  25. *       searchNameNet()         search net on Name
  26. */
  27. extern FILE *netLog;
  28. /*
  29. * getNet()
  30. *
  31. * This function gets a net node from the file.
  32. */
  33. void getNet(int n, NetBuffer *buf)
  34.   {
  35.   long int r, s;
  36.   char error[50];
  37.   if (buf == &netBuf)
  38.   thisNet = n;
  39.   r = NB_TOTAL_SIZE;
  40.   s = n * r;
  41.   fseek(netfl, s, 0);
  42.   if (fread(buf, NB_SIZE, 1, netfl) != 1)
  43.     {
  44.     sPrintf(error, "?getNet-read fail(1) for slot %d.", n);
  45.     crashout(error);
  46.  
  47.     }
  48.   crypte(buf, NB_SIZE, n);
  49.   if (SR_BULK != 0)
  50.     {
  51.     if (fread(buf->netRooms, SR_BULK, 1, netfl) != 1)
  52.       {
  53.       sPrintf(error, "?getNet-read fail(2) for slot %d.", n);
  54.       crashout(error);
  55.  
  56.       }
  57.  
  58.     }
  59.  
  60.   }
  61. /*
  62. * putNet()
  63. *
  64. * This will put a node to file.  It also updates the net index.
  65. */
  66. void putNet(int n, NetBuffer *buf)
  67.   {
  68.   long int r, s;
  69.   label temp;
  70.   char error[50];
  71.   if (buf == &netBuf) thisNet = n;
  72.   copy_struct(buf->nbflags, netTab[n].ntflags);
  73.   memcpy(netTab[n].netTRooms, buf->netRooms, (long)SR_BULK);
  74.   netTab[n].ntnmhash = hash(buf->netName);
  75.   normId(buf->netId, temp);
  76.   netTab[n].ntidhash = hash(temp);
  77.   netTab[n].ntMemberNets = buf->MemberNets;
  78.   strCpy(netTab[n].ntShort, buf->nbShort);
  79.   netTab[n].ntGen = buf->nbGen;
  80.   r = NB_TOTAL_SIZE;
  81.   s = n * r;
  82.   crypte(buf, NB_SIZE, n);
  83.   fseek(netfl, s, 0);
  84.   if (fwrite(buf, NB_SIZE, 1, netfl) != 1)
  85.     {
  86.     sPrintf(error, "?putNet-write fail(1), slot %d!!", n);
  87.     crashout(error);
  88.  
  89.     }
  90.   if (SR_BULK != 0)
  91.   if (fwrite(buf->netRooms, SR_BULK, 1, netfl) != 1)
  92.     {
  93.     sPrintf(error, "?putNet-write fail(2), slot %d!!", n);
  94.     crashout(error);
  95.  
  96.     }
  97.   crypte(buf, NB_SIZE, n);
  98.  
  99.   }
  100. /*
  101. * normId()
  102. *
  103. * This normalizes a node id, i.e., kills all the punctuation and uppercases
  104. * the country designation.
  105. */
  106. char normId(label source, label dest)
  107.   {
  108.   int digitcount = 0;
  109.   while ( !isalpha( *source ) && *source )  source++;
  110.   if ( !*source ) return FALSE;
  111.   *dest++   =   toUpper(*source++);
  112.   while ( !isalpha( *source ) && *source )  source++;
  113.   if ( !*source ) return FALSE;
  114.   *dest++   =   toUpper(*source++);
  115.   while (*source)
  116.     {
  117.     if (isdigit(*source))
  118.       {
  119.       *dest++ = *source;
  120.       digitcount++;
  121.  
  122.       }
  123.     source++;
  124.  
  125.     }
  126.   *dest = '\0';
  127.   return ( char )(digitcount > 8);
  128.  
  129.   }
  130. /*
  131. * searchNet()
  132. *
  133. * This function Searches the net for the given Id.
  134. */
  135. int searchNet(char *forId, NetBuffer *buf)
  136.   {
  137.   int   rover;
  138.   label temp, temp2;
  139.   normId(forId, temp2);
  140.   for (rover = 0; rover < cfg.netSize; rover++)
  141.     {
  142.     if (netTab[rover].ntflags.in_use &&
  143.     hash(temp2) == netTab[rover].ntidhash)
  144.       {
  145.       getNet(rover, buf);
  146.       normId(buf->netId, temp);
  147.       if (strCmpU(temp, temp2) == SAMESTRING)
  148.       return rover;
  149.  
  150.       }
  151.  
  152.     }
  153.   return ERROR;
  154.  
  155.   }
  156. /*
  157. * searchNameNet()
  158. *
  159. * This function will search the  net for given node name.  This includes checks
  160. * for both full and shorthand names.
  161. */
  162. int searchNameNet(label name, NetBuffer *buf)
  163.   {
  164.   int rover;
  165.   if (strlen(name) == 0) return ERROR;
  166.   /* First check for full name */
  167.   for (rover = 0; rover < cfg.netSize; rover++)
  168.     {
  169.     if (netTab[rover].ntflags.in_use &&
  170.     hash(name) == netTab[rover].ntnmhash)
  171.       {
  172.       getNet(rover, buf);
  173.       if (strCmpU(buf->netName, name) == SAMESTRING)
  174.       return rover;
  175.  
  176.       }
  177.  
  178.     }
  179.   /* Now check for shorthand name */
  180.   for (rover = 0; rover < cfg.netSize; rover++)
  181.   if (netTab[rover].ntflags.in_use &&
  182.   strCmpU(netTab[rover].ntShort, name) == SAMESTRING)
  183.     {
  184.     getNet(rover, buf);
  185.     return rover;
  186.  
  187.     }
  188.   return ERROR;
  189.  
  190.   }
  191.